<?php

namespace app\serversetting\service\setting;
use app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-06-21 14:14:33
 * @Description: 全局设置分类
 * 关于分类：    
 		子类与父类的app_id和other_app_read的这两个字段保持相同
        父类不可修改的字段： app_id pid （父类修改other_app_read时，子类陪同）
        子类不可修改的字段： app_id pid other_app_read
        前台获取类别： app_id or other_app_read 下的分类
        后台获取某应用下的的类别： app_id 下的分类（若app_id为亚数11的话，获取所有应用下的分类）
 */

class SettingCategory extends ApiBase
{
	
	protected $settingCategoryLogic = '\app\serversetting\logic\SettingCategory';
    protected $settingCategoryValidate = '\app\serversetting\validate\SettingCategory';
    protected $addFields = ['app_id'=>0,'other_app_read'=>'','is_public'=>' ','pid'=>0,'name'=>' ','action'=>' ','order'=>100];
    protected $haveEditFields = ['app_id'=>0,'other_app_read'=>'','is_public'=>' ','id'=>0,'name'=>' ','action'=>'','order'=>100];

    use \app\servergoods\traits\BaseCheckCate;

    /*添加一个分类*/
    public function addCategory(): array 
    {
        $data = $this->receiveData($this->addFields);

    	$checkData = $this->_baseCheckCate($data, '', 'edit', $this->settingCategoryValidate, $this->settingCategoryLogic);
    	if($checkData['status'] != SUCCESS_STATUS){
    		return $checkData;
    	}
		$data['level'] = $data['pid'] ? $checkData['res']['level'] + 1 : 0;
		return $this->logic($this->settingCategoryLogic)->addCategory($data);
    }

    /*编辑一个分类*/
    public function editCategory(): array 
    {
        $data = $this->issetReceiveData($this->haveEditFields);

    	$checkData = $this->_baseCheckCate($data, '', 'edit', $this->settingCategoryValidate, $this->settingCategoryLogic);
    	if($checkData['status'] != SUCCESS_STATUS){
    		return $checkData;
    	}

    	return $this->logic($this->settingCategoryLogic)->editCategory($data);
    }

    /*获取分类树*/
    public function getTree(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data,'getTree','canNoId');
    }

    /*单个操作或批量操作分类的检验数据、分类的存在性、该应用是否可操作*/
    protected function _batchCheckCate($data, $scene = ''): array 
    {

        $validate = validate($this->settingCategoryValidate);
        if(!$validate->scene($scene)->check($data)){
            return verify_err(null, $validate->getError());
        }

        is_numeric($data['id']) && $data['id'] = [$data['id']];
        $cateList = $this->logic($this->settingCategoryLogic)->dbCateList(['id'=>['in',$data['id']]]);

        if(!$cateList){
            return verify_err(null, '所选择的分类不存在');
        }
        if(count($cateList) != count($data['id'])){
            return verify_err(null, '所选择的分类个数与所查询到的分类个数不统一');
        }
        return auto_success($data, '可操作选择的分类');
    }

    /*查看某个分类详情*/
    public function oneCateInfo(): array 
    {
        $data = $this->receiveData(['id'=>0,'app_id'=>0]);
        $checkData = $this->_batchCheckCate($data, 'checkId');
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }
        return $this->logic($this->settingCategoryLogic)->oneCateInfo($data);
    }

    /*批量删除分类*/
    protected function _batchDelCate($data, bool $isBatch = false): array 
    {
        $scene = $isBatch ? 'checkIds' : 'checkId';

        $checkCate = $this->_batchCheckCate($data, $scene);
        if($checkCate['status'] != SUCCESS_STATUS){
            return $checkCate;
        }

        is_numeric($data['id']) && $data['id'] = [$data['id']];
        $list = $this->logic($this->settingCategoryLogic)->dbCateList(['pid'=>['in',$data['id']]]);
        if(count($list) > 0){
            return auto_failure(null,'失败，存在子类【' . $list[0]['name'] . '】');
        }

        return $this->logic($this->settingCategoryLogic)->batchDelCate($data);
    }

    /*删除某个分类*/
    public function delOneCate(): array 
    {
        $data = $this->receiveData(['id'=>0,'app_id'=>0]);
        return $this->_batchDelCate($data, false);
    }

    /*批量删除选中的分类*/
    public function batchDelCate(): array 
    {
        $data = $this->receiveData(['id'=>[],'app_id'=>0]);
        return $this->_batchDelCate($data, true);
    }

    /*启用/禁用 一个 分类*/
    public function changeStatus(): array 
    {
        $data = $this->receiveData(['id'=>0,'app_id'=>0,'status'=>' ']);
        return $this->_batchChangeSta($data, false);
    }

    /*启用/禁用 多个 分类*/
    public function batchChangeStatus(): array 
    {
        $data = $this->receiveData(['id'=>[],'app_id'=>0,'status'=>' ']);
        return $this->_batchChangeSta($data, true);
    }

    /*启用/禁用 一个/多个 分类*/
    protected function _batchChangeSta($data, bool $isBatch = false): array 
    {
        $scene = $isBatch ? 'batchChangeStatus' : 'changeStatus';

        $checkCate = $this->_batchCheckCate($data, $scene);
        if($checkCate['status'] != SUCCESS_STATUS){
            return $checkCate;
        }

        return $this->logic($this->settingCategoryLogic)->batchChangeStatus($data);
    }




    

}






